package com.woniuxy.carlife.controller;

import com.alibaba.fastjson.JSON;
import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.Result;
import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.StatusCode;
import com.woniuxy.carlife.dto.CouponCategoryDto;
import com.woniuxy.carlife.feigndto.StoreServiceDto;
import com.woniuxy.carlife.feignservice.StoreFeignService;
import com.woniuxy.carlife.service.CouponCategoryService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author fx
 * @ClassName:CouponCategoryController
 * @Description: 优惠券类别控制类
 * @date 2021/4/6 20:00
 **/

@RestController
@RequestMapping("/couponCategory")
@Slf4j
@Api(tags = "优惠券类别接口信息")//用于描述接口类的相关信息，作用于类上
public class CouponCategoryController {

    //日志对象，加了@Slf4j就不需要这句话了
//    private Logger logger= LoggerFactory.getLogger(CouponCategoryController.class);

    @Resource
    private CouponCategoryService couponCategoryService;

    @Resource
    private RedisTemplate redisTemplate;


    /**
     * 功能描述〈 增加优惠券类别〉
     * @author:fx
     * @param: @RequestBody String couponCategory
     * @return: Result
     * @Date 2021/4/6 22:27
     * @throws
     */
    //@ApiOperation用于描述接口方法，作用于方法上
    @ApiOperation(value = "新增优惠券类别",notes = "<span style='color:red;'>用来新增优惠券类别</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 20000,message = "新增成功"),
            @ApiResponse(code=20001,message = "新增失败"),
            @ApiResponse(code=30001,message = "输入参数有误")
    })
    //@ApiImplicitParams用于描述接口参数
    @ApiImplicitParams({
            //dataType:参数类型
            //paramType:参数由哪里获取     path->从路径中获取，query->?传参，body->ajax请求
            @ApiImplicitParam(name = "couponCategory",value = "优惠券类别名称",dataType = "String",paramType = "query",example = "满减"),
    })
    @PostMapping("/platformmanage/couponcategory")
    public Result saveCouponCategory(String couponCategory){
        if (couponCategory==null||couponCategory.trim().length() == 0 ){
            return new Result(false,StatusCode.PARAMERROR,"输入参数有误");
        }
        //把参数在日志中打印出来
        log.info("saveCouponCategory开始:参数:{}",couponCategory);
        //执行新增
        int i = couponCategoryService.saveCouponCategory(couponCategory);
        if (i==1) {
            log.info("saveCouponCategory结果:{}","新增成功");
            return new Result(true, StatusCode.OK,"新增成功");
        }else if (i==-1){
            log.info("saveCouponCategory结果:{}","新增失败，类别名已存在");
            log.info("saveCouponCategory结束:参数:{}",couponCategory);
            return new Result(false, StatusCode.ERROR,"新增失败，类别名已存在");
        }
        log.info("saveCouponCategory结果:{}","新增失败");
        log.info("saveCouponCategory结束:参数:{}",couponCategory);
        return new Result(false, StatusCode.ERROR,"新增失败");
    }

    /**
     * 功能描述〈获得所有优惠券类别〉
     * @author:fx
     * @param: null
     * @return: Result
     * @Date 2021/4/6 22:35
     * @throws
     */
    //@ApiOperation用于描述接口方法，作用于方法上
    @ApiOperation(value = "获取所有优惠券类别名称和id",notes = "<span style='color:red;'>用来获取所有优惠券类别名称和id的接口，不要参数</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 20000,message = "查询成功",response = CouponCategoryDto.class),
            @ApiResponse(code=20001,message = "查询失败")
    })
    @GetMapping("/platformmanage/couponcategory")
    public Result findCouponCategory(){
        log.info("findCouponCategory开始");
        //这里只返回了优惠券类别名称和id
        List<CouponCategoryDto> couponCategory = couponCategoryService.findCouponCategory();
        if (ObjectUtils.isEmpty(couponCategory)){
            log.info("findCouponCategory结束:{}","查询失败");
            return new Result(false,StatusCode.ERROR,"没有查询到数据");
        }
        log.info("findCouponCategory结束:{}","查询成功");
        return new Result(true,StatusCode.OK,"查询成功",couponCategory);
    }

    /**
     * 功能描述〈根据优惠券类别id更改指定的优惠券类别状态〉
     * @author:fx
     * @param: Integer id:类别的id
     * @return:
     * @Date 2021/4/6 22:37
     * @throws
     */
    @PutMapping("/platformmanage/updatecouponcategory")
    //@ApiOperation用于描述接口方法，作用于方法上
    @ApiOperation(value = "更改优惠券类别状态",notes = "<span style='color:red;'>用来更改优惠券类别状态</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 20000,message = "修改成功"),
            @ApiResponse(code=20001,message = "修改失败"),
            @ApiResponse(code=30001,message = "输入参数有误")
    })
    //@ApiImplicitParams用于描述接口参数
    @ApiImplicitParams({
            //dataType:参数类型
            //paramType:参数由哪里获取     path->从路径中获取，query->?传参，body->ajax请求
            @ApiImplicitParam(name = "id",value = "优惠券类别id",dataType = "Integer",paramType = "query",example = "1"),
    })
    public Result updateCouponCategoryStatus(Integer id){
        log.info("updateCouponCategoryStatus开始");
        if (id==null||id<=0){
            log.info("updateCouponCategoryStatus结束:{}","输入参数为空或者小于1");
            return new Result(false,StatusCode.PARAMERROR,"输入参数有误");
        }else if (ObjectUtils.isEmpty(couponCategoryService.getById(id))){
            log.info("updateCouponCategoryStatus结束:{}","根据输入参数不能在数据库中查找到信息");
            return new Result(false,StatusCode.ERROR,"数据不存在");
        }
        if (couponCategoryService.updateCouponCategoryStatus(id)) {
            log.info("updateCouponCategoryStatus结束:{}","修改成功");
            return new Result(true,StatusCode.OK,"修改状态成功");
        }
        log.info("updateCouponCategoryStatus结束:{}","修改失败");
        return new Result(false,StatusCode.ERROR,"修改状态失败");
    }
}

